﻿using Rescue.Services;
using RescueApp.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RescueApp.Repository;
using RescueApp.Models;
using RescueApp.UI;
using Rescue.UI.Common;

namespace Rescue.UI
{
    public partial class RescueApplyDetails : Form
    {


        private RescueContext db;
        private RescueRepos repos;
        private RescueApplyService srv;
        public RescueApplyDetails()
        {
            InitializeComponent();
        }
        public RescueApplyDetails(RescueContext db)
        {
            InitializeComponent();
            this.repos = new RescueRepos();
            this.db = db;
            srv = new RescueApplyService(db, repos);
        }

        private void loadData(DataGridView dg)
        {
            List<RescueApply> todoItems = srv.QueryAllItems();
            dg.DataSource = todoItems;
            dg.Refresh();
        }




        private void button1_Click(object sender, EventArgs e)//填写按钮
        {
            RescueApplyNewItem frm = new RescueApplyNewItem(db, srv);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Form1_Load(this, null);
            }
            frm.Close();
        }

        private void button2_Click(object sender, EventArgs e)//修改按钮
        {
            int idx;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择你想要修改的数据！", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            idx = dataGridView1.SelectedRows[0].Index;
            int Id = int.Parse(dataGridView1.Rows[idx].Cells[0].Value.ToString());
            string showId = dataGridView1.Rows[idx].Cells[0].Value.ToString();
            textBox1.Text = showId;
            var todoitem = db.RescueApplies.FirstOrDefault(x => x.RescueID == Id);
            if (todoitem == null)
            {
                MessageBox.Show($"not Found the Id:{Id} Item!");
                return;
            }

            RescueApplyUpdate frm = new RescueApplyUpdate(db, srv, todoitem);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Form1_Load(this, null);
            }
            frm.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)//查询按钮
        {
            DispatchResult frm = new DispatchResult(db, srv);
            frm.ShowDialog();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loadData(dataGridView1);
        }

        private void button4_Click(object sender, EventArgs e)//刷新按钮
        {
            loadData(dataGridView1);
        }

        private void button3_Click(object sender, EventArgs e)//删除按钮
        {
            int idx;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择你想要修删除的数据！", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            idx = dataGridView1.SelectedRows[0].Index;
            int Id = int.Parse(dataGridView1.Rows[idx].Cells[0].Value.ToString());
            string showId = dataGridView1.Rows[idx].Cells[0].Value.ToString();
            textBox1.Text = showId;

            if (MessageBox.Show($"确定要删除救援申请ID:{Id} 吗?", "确认消息", MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                return;
            }
            if (!DbHelper.IsExistsWithORM(db, Id))
            {
                MessageBox.Show($"该救援申请ID:{Id} 不存在!");
                return;
            }
            var entity = db.RescueApplies.FirstOrDefault(x => x.RescueID == Id);
            if (entity != null)
            {
                try
                {
                    //db.RescueApplies.Remove(entity);
                    //db.SaveChanges();
                    srv.Delete(entity);
                    MessageBox.Show($"救援申请ID:{Id} 删除成功!");
                    Form1_Load(this, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)//显示当前ID
        {


        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
